package com.goodboys.controller;

import com.alibaba.fastjson.JSON;
import com.goodboys.base.controller.BaseController;
import com.goodboys.base.entity.BaseReqBean;
import com.goodboys.base.resp.Response;
import com.goodboys.base.webscoket.SocketSessionRegistry;
import com.goodboys.entity.Info;
import com.goodboys.entity.LngLat;
import com.goodboys.entity.User;
import com.goodboys.service.InfoService;
import com.goodboys.service.UserService;
import com.goodboys.utils.BaiduLocationUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by CJ on 2018-05-28.
 */
@Controller
public class WebScoketController extends BaseController{

    @Autowired
    private InfoService infoService;

    @Autowired
    private UserService userService;
    /**session操作类*/
    @Autowired
    SocketSessionRegistry webAgentSessionRegistry;
    /**消息发送工具*/
    @Autowired
    private SimpMessagingTemplate template;

    //监控新增用户
//    @MessageMapping("/register")

   /* @PostMapping("/sys/register")
    @ResponseBody
    public  Response  register(User user) throws Exception {
        Map map = userService.register(user);
        //发送至目标用户
        if((Integer)map.get("status")==1) {
            User temp = (User) map.get("status");
            webAgentSessionRegistry.getAllSessionIds().entrySet().stream()
                    .map(Map.Entry::getValue).flatMap(users -> users.stream().collect(Collectors.toList()).stream())
                    .forEach(user2 -> template.convertAndSendToUser(user.getSessionId(),"/topic/publish", JSON.toJSONString(map),createHeaders(user.getSessionId())));
        }
        return getRetunResult(map, new Response());
    }*/

    //监控信息发布
//    @MessageMapping("/publish")
   /* @PostMapping("/info/publish")
    @ResponseBody
    public  Response  publish(BaseReqBean baseReqBean) throws Exception {
        Map map = infoService.publish(baseReqBean);
        //返回给当前用户
        webAgentSessionRegistry.getSessionIds(baseReqBean.getId()).stream().forEach(user -> {
            template.convertAndSendToUser(user.getSessionId(),"/topic/publish", JSON.toJSONString(map),createHeaders(user.getSessionId()));
        });
        //发送至目标用户
        if((Integer)map.get("status")==1 && map.get("message") instanceof Info) {
            Info message = (Info)map.get("message");
            String content = message.getContent();
            String title = message.getTitle();
            webAgentSessionRegistry.getAllSessionIds().entrySet().stream()
//                    .filter(stringSetEntry -> stringSetEntry.getKey().equals(baseReqBean.getId()))
                    .map(Map.Entry::getValue).flatMap(users -> users.stream().collect(Collectors.toList()).stream())
                    .filter(user -> user.getType() == baseReqBean.getType())
                    .filter(user -> user.getType() == baseReqBean.getType())
                    .filter(user -> user.getTag() == null || user.getTag() == baseReqBean.getTag())
                    .filter(user -> user.getRange() <= BaiduLocationUtils.getDistance(new LngLat(user.getLng(), user.getLat()), new LngLat(baseReqBean.getLng(), baseReqBean.getLat())))
                    .filter(user -> (StringUtils.isEmpty(user.getKeyword()) || content.contains(user.getKeyword()) || title.contains(user.getKeyword())))
                    .forEach(user -> template.convertAndSendToUser(user.getSessionId(),"/topic/publish", JSON.toJSONString(map),createHeaders(user.getSessionId())));
        }
        return getRetunResult(map, new Response());
    }*/






}
